package com.spring.sunflower.main.presenter;

import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;
import com.spring.sunflower.base.BaseObserver;
import com.spring.sunflower.base.BasePresenter;
import com.spring.sunflower.bean.CommonBean;
import com.spring.sunflower.bean.NearbyBean;
import com.spring.sunflower.main.MainContract;

import java.util.Map;

import io.reactivex.rxjava3.core.Observable;

/**
 * Created by Kevin on 2021/6/1<br/>
 * <p>
 * Describe:<br/>
 */
public class NearbyPresenter extends BasePresenter<MainContract.NearbyView> {
    private String TAG = "NearbyPresenter";

    public NearbyPresenter(MainContract.NearbyView baseView) {
        super(baseView);
    }

    public void getNearby(int pageIndex) {
        Observable<NearbyBean> observable = requestDataSource.requestNearby(pageIndex);
        addDisposable(observable, new BaseObserver<NearbyBean>(baseView) {

            @Override
            public void onSuccess(NearbyBean bean) {
                String json = new Gson().toJson(bean);
                LogUtils.dTag(TAG, "getNearby=" + json);
                baseView.hideLoading();
                baseView.showNearbyData(bean.getDatas());
            }

            @Override
            public void onError(int status, String msg) {

            }
        });
    }

    public void updateLocation(Map<String, String> map) {
        Observable<CommonBean> observable = requestDataSource.updateLocation(map);
        addDisposable(observable, new BaseObserver<CommonBean>() {
            @Override
            public void onSuccess(CommonBean bean) {
                String json = new Gson().toJson(bean);
                LogUtils.dTag(TAG, "getNearby=" + json);
                baseView.updateLocationSuccess();
            }

            @Override
            public void onError(int status, String msg) {
                baseView.updateLocationFailed();
            }
        });
    }
}
